<template>
  <div class="app-container">
    <!--课程名称-->
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item label="课程名称">
        <el-input v-model="queryParam.title" placeholder="课程名称" />
      </el-form-item>
      <!-- 课程讲师 -->
      <el-form-item label="课程讲师">
        <el-select v-model="queryParam.teacherId" placeholder="请选择">
          <el-option
            v-for="teacher in teacherList"
            :key="teacher.id"
            :label="teacher.name"
            :value="teacher.id"
          />
        </el-select>
      </el-form-item>
      <!-- 一级分类 -->
      <el-form-item label="课程类别">
        <el-select
          v-model="queryParam.subjectParentId"
          @change="getTwoSubjectChange"
          placeholder="课程类别"
        >
          <el-option
            v-for="subject in subjectOneList"
            :key="subject.id"
            :label="subject.title"
            :value="subject.id"
          />
        </el-select>
        <!-- 二级分类 -->
        <el-select v-model="queryParam.subjectId" placeholder="二级分类">
          <el-option
            v-for="subject in subjectTwoList"
            :key="subject.value"
            :label="subject.title"
            :value="subject.id"
          />
        </el-select>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getListEntity()"
        >查询</el-button
      >
      <el-button type="default" @click="resetData()">清空</el-button>
      <router-link :to="{ path: '/course/info' }">
        <el-button type="primary" icon="el-icon-plus">添加课程</el-button>
      </router-link>
    </el-form>
    <!-- ==============表单===================== -->
    <template>
      <el-table
        :data="info"
        style="width: 100%"
        :default-sort="{ prop: 'date', order: 'descending' }"
      >
        <el-table-column prop="title" label="课程标题" width="180">
        </el-table-column>
        <el-table-column prop="price" label="价格"> </el-table-column>浏览数量
        <el-table-column prop="lessonNum" label="总课时"> </el-table-column>
        <el-table-column prop="viewCount" label="浏览数量"> </el-table-column>
        <el-table-column prop="buyCount" label="销售数量"> </el-table-column>

        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status === 'Draft' ? 'primary' : 'success'"
              disable-transitions
              >{{ scope.row.status === "Draft" ? "未发布" : "已发布" }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="gmtCreate" sortable label="创建时间">
        </el-table-column>
        <el-table-column prop="gmtModified" label="修改时间"> </el-table-column>

        <el-table-column label="操作">
          <template slot-scope="scope">
            <router-link :to="{ path: '/course/info/' + scope.row.id }">
              <el-button type="info" round>修改章节</el-button>
            </router-link>
            <router-link :to="{ path: '/course/chapter/' + scope.row.id }">
              <el-button type="info" round>修改小节</el-button>
            </router-link>
            
            <el-button
              type="danger"
              @click="deleteEntityById(scope.row.id)"
              round
              >删除课程</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="tolal"
      >
      </el-pagination> </template
    >>
  </div>
</template>
  
  <script>
import courseApi from "@/api/edu/course";
import teacherApi from "@/api/edu/teacher";
import subjectApi from "@/api/edu/subject";
export default {
  data() {
    return {
      page: 1,
      limit: 5,
      // 条件查询参数
      queryParam: {
        title: "",
        teacherId: "",
        subjectParentId: "",
        subjectId: "",
        status: "",
      },
      // 讲师列表
      teacherList: [],
      // 一级分类
      subjectOneList: [],
      // 二级分类
      subjectTwoList: [],
      tolal: 0,
      info: [],
    };
  },
  created() {},
  mounted() {
    this.init();
  },

  methods: {
    init() {
      this.listEntity();
      this.getOneSubject();
      this.getListEntity();
    },
    // 获取所有的数据
    getListEntity() {
      courseApi
        .listEntity(this.queryParam, this.page, this.limit)
        .then((res) => {
          this.info = res.data.info;
          this.tolal = res.data.total;
        })
        .catch((error) => {
          console.log(error);
        });
    },
    // 设置页面大小
    handleSizeChange(val) {
      this.limit = val;
      this.getListEntity();
    },
    // 页面切换
    handleCurrentChange(val) {
      this.page = val;
      this.getListEntity();
    },
    resetData() {
      //清空的方法
      //表单输入项数据清空
      this.queryParam = {};
      //查询所有讲师数据
      this.getListEntity();
    },
    //删除讲师的方法
    deleteEntityById(id) {
      this.$confirm("此操作将永久删除该条记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //点击确定，执行then方法
          //调用删除的方法
          courseApi.deleteEntityById(id).then((response) => {
            //删除成功
            //提示信息
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            //回到列表页面
            this.getListEntity();
          });
        })
        //点击取消，执行catch方法
        .catch((error) => {
          this.$message("取消删除");
        });
    },
    // =======================================================查询条件+++++++++++++++++++++++++++++
     // 查询所有的讲师
     listEntity() {
      teacherApi.listEntity().then((res) => {
        this.teacherList = res.data.info;
      });
    },
    // 获取一级分类
    getOneSubject() {
      subjectApi.getTreeEntity().then((res) => {
        this.subjectOneList = res.data.info;
      });
    },
    // 获取二级分类
    getTwoSubjectChange(value) {
      console.log("查询二级分类");
      this.subjectOneList.forEach((item) => {
        // 判断该id是否等于
        if (item.id == value) {
          // 获取二级分类
          this.subjectTwoList = item.children;
          // 清空二级分类
          this.queryParam.subjectId = "";
        }
      });
    },

  },
};
</script>
  
  <style>
</style>